package rrs;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.StringTokenizer;
import java.sql.*;

/* read in the SystemInitialization file to set up the products in the
   inventory table in the database.
 */
public class SystemInitialize
{
    public static boolean initialize(String textFile)
    {
        boolean readInOk = true;
        String line = "";
        try
        {
              FileReader fileReader = new FileReader (textFile);
              BufferedReader in = new BufferedReader (fileReader);
              line = in.readLine();
              while (line != null)
              {
                  while (ignoreLine(line))
                      line = in.readLine();
                  
                  
                  if (line.equals("<products>"))
                  {
                     while (!line.equals("</products>"))
                     {
                         line = in.readLine();
                         
                         while (ignoreLine(line))
                             line = in.readLine();
                          
                         if (line.equals("</products>"))
                             break;
                         
                         insertProduct(line);
                     } // end while
                  } // end if
                  else
                     line = in.readLine();
              } // end while           
        } // end try
        catch (IOException e)
        {
            readInOk = false;
        } // end catch 
        
        return readInOk;
    } // end initialize
    
    // return true if its a line to ignore
    private static boolean ignoreLine(String line)
    {
        if (line.length() == 0 || line.charAt(0) == '#')
            return true;
        else
            return false;
    } // end ignoreLine
    
    // get the product id from the line.
    private static String getProductID(String line)
    {
        String result = "";
        StringTokenizer parser = new StringTokenizer(line);
        result = parser.nextToken();
        return result;
    } // end getProductID
    
    // get the product description from the line
    private static String getProductDescription(String line)
    {
        String result = "";
        System.out.println(line);
        
        for (int i = 0; i < line.length(); i++)
        {
           if (line.charAt(i) == '<')
           {
               i++;
               while (line.charAt(i) != '>')
               {
                  result = result + line.charAt(i);
                  i++;
               } // end while
           } // end if
        } // end for
        
        return result;
    } // end getProductDescription
    
    // get the product price from the line
    private static String getProductPrice(String line)
    {
        int linePosition = 0;
        String result = "";
        
        for (int i = 0; i < line.length(); i++)
        {
            if (line.charAt(i) == '>')
                linePosition = i + 1;
        } // end for
        
        linePosition++;
        while (line.charAt(linePosition) != ' ')
        {
            result = result + line.charAt(linePosition);
            linePosition++;
        } // end while
        
        result = result.trim();
        return result;
    } // end getProductPrice
    
    // get the product qauntity from the line
    private static String getProductQuantity(String line)
    {        
       int linePosition = 0;
        String result = "";
        
        for (int i = 0; i < line.length(); i++)
        {
            if (line.charAt(i) == '>')
                linePosition = i + 1;
        } // end for
        
        linePosition++;
        while (line.charAt(linePosition) != ' ')
        {
            result = result + line.charAt(linePosition);
            linePosition++;
        } // end while
        
        result = line.substring(linePosition);
        
        result = result.trim();
        return result;
    } // end getProductQuantity
    
    // insert the product into the database
    private static void insertProduct(String line)
    {
        String ID = getProductID(line);
        String description = getProductDescription(line);
        String price = getProductPrice(line);
        String quantity = getProductQuantity(line);
        try
        {
           Connection conn = DBInterface.dbConnOpen();
           System.out.println("The connection = " + conn);
	   Statement stmt = conn.createStatement();
	   String insertStatement = "INSERT INTO Inventory VALUES('" + ID +
                                    "', " + price + ", " + quantity + ", " +
                                    "'" + description + "')";
          // System.out.println(insertStatement);
           stmt.executeUpdate(insertStatement);
           conn.commit();
        } // end try
        catch (SQLException e)
        {
            System.out.println("Caught SQLException in SystemInitalize." +
                               "insertProduct" + e);
        } // end catch
        DBInterface.dbConnClose();
       // System.out.println("\nID is " + ID + " description is " + description + " price is " + price + " quantity is " + quantity);
    } // end insertProduct
} // end class SystemInitialize